package network;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import packets.Dispatcher;

/**
 * @TODO Allow only to listen to one host.
 * @TODO Support for setting maximum number of connections.
 */
public class NewConnectionListener implements Runnable {

	@SuppressWarnings("unused")
	private String host;
	private int port;
	@SuppressWarnings("unused")
	private int maxconnections;
	private Dispatcher dispatcher;
	
	public NewConnectionListener(Dispatcher dispatcher, String host, int port, int maxconnections) {
		this.dispatcher = dispatcher;
		this.host = host;
		this.port = port;
		this.maxconnections = maxconnections;
	}
	
	@Override
	public void run() {
		try {
			ServerSocket server = new ServerSocket(port);
			
			while(true) {
				Socket socket = server.accept();
				System.out.println("Got connection!");
				new Thread(new MCConnection(socket, dispatcher)).start();
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
